package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zcf
 */
@RestController
@RequestMapping("admin/category")
@Api(tags = "分类管理")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     * 新增分类
     */
    @PostMapping()
    @ApiOperation("新增分类")
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /**
     * 分页查询
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据类型查询分类
     */
    @GetMapping("list")
    @ApiOperation("根据类型查询分类")
    public Result list(Integer type) {

       List<Category> categories= categoryService.list(type);
       return Result.success(categories);

    }


    /**
     * 修改分类
     */
    @PutMapping()
    @ApiOperation("修改分类")
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        categoryService.update(categoryDTO);
        return Result.success();
    }


    /**
     * 启用禁用
     */
    @PostMapping("status/{status}")
    @ApiOperation("启用禁用")
    public Result enableOrDisable(@PathVariable Integer status, Long id) {
        categoryService.enableOrDisable(status, id);
        return Result.success();
    }


    /**
     * 根据id删除分类
     */
    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result deleteById(Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }
}
